package com.tian.recycler.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.tian.recycler.R
import com.tian.recycler.databinding.FragmentItemTouchBinding
import com.tian.recycler.holder.SimpleDividerDecoration
import com.tian.recycler.itemtouch.ItemTouchAdapter
import com.tian.recycler.itemtouch.ItemTouchCallback


/**
 * A simple [Fragment] subclass.
 * Use the [ItemTouchFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ItemTouchFragment : Fragment() {

    private lateinit var b: FragmentItemTouchBinding

    private val data: MutableList<String> by lazy {
        val temp = mutableListOf<String>()
        for (i in 0 until 20) {
            temp.add("songtao $i")
        }
        temp
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        b = DataBindingUtil.inflate(inflater, R.layout.fragment_item_touch, container, false)
        b.rvItemTouch.layoutManager = LinearLayoutManager(requireContext())
        val adapter = ItemTouchAdapter(data)
        b.rvItemTouch.adapter = adapter
        b.rvItemTouch.addItemDecoration(SimpleDividerDecoration(Color.WHITE))
        val itemTouchCallback = ItemTouchCallback().apply { itemTouchAdapterCallback = adapter }
        val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
        itemTouchHelper.attachToRecyclerView(b.rvItemTouch)
        return b.root
    }


}